package com.example.spring_boot_demo.service.impl;

import com.example.spring_boot_demo.dto.User;
import com.example.spring_boot_demo.exception.StudyException;
import com.example.spring_boot_demo.mapper.UserMapper;
import com.example.spring_boot_demo.service.UserService;
import com.example.spring_boot_demo.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@SuppressWarnings({"unchecked", ""})
@Service
public class UserServiceImpl implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);


    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {

        User account = userMapper.findUserByAccount(user);


        if (account == null) {
           throw new StudyException("账户不存在");
        }

        if (!account.getPassword().equals(user.getPassword())) {
            throw new StudyException("密码错误");
        }

        String token = JWTUtil.generateToken(account.getId());
        account.setToken(token);

        return account;
    }
}
